package tw.zld369504.testmediaplayer;

import android.support.v4.widget.ContentLoadingProgressBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.pili.pldroid.player.PLMediaPlayer;
import com.pili.pldroid.player.widget.PLVideoTextureView;
import com.pili.pldroid.player.widget.PLVideoView;

import static tw.zld369504.testmediaplayer.GlobalConfig.url;
import static tw.zld369504.testmediaplayer.GlobalConfig.url1;

public class PLDroidPlayerActivity extends AppCompatActivity implements PLMediaPlayer.OnErrorListener {

    private PLVideoTextureView mVideoView;
    private ContentLoadingProgressBar loadingView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pldroid_player);
        mVideoView = (PLVideoTextureView) findViewById(R.id.PLVideoTextureView);
        loadingView = (ContentLoadingProgressBar)findViewById(R.id.loading);
        mVideoView.setVideoPath(url);
        mVideoView.setBufferingIndicator(loadingView);
        mVideoView.setOnErrorListener(this);
        mVideoView.setKeepScreenOn(true);
        mVideoView.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mVideoView.stopPlayback();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mVideoView.pause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mVideoView.start();
    }

    @Override
    public boolean onError(PLMediaPlayer plMediaPlayer, int i) {
        switch (i) {
            case PLMediaPlayer.ERROR_CODE_IO_ERROR:
                Log.d("234dsf", "ERROR_CODE_IO_ERROR网络异常");
                plMediaPlayer.stop();
                plMediaPlayer.reset();
                return true;
            case PLMediaPlayer.ERROR_CODE_404_NOT_FOUND:
                Log.d("234dsf", "ERROR_CODE_404_NOT_FOUND播放资源不存在");
                break;
            case PLMediaPlayer.ERROR_CODE_READ_FRAME_TIMEOUT:
                Log.d("234dsf", "ERROR_CODE_READ_FRAME_TIMEOUT连接超时");
                break;
        }
        return true;
    }
}
